package com.fendou.virtualphone.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.text.format.DateFormat;

import com.fendou.virtualphone.activity.PhoneIncomingActivity;
import com.fendou.virtualphone.bean.CallBeanInfo;
import com.fendou.virtualphone.db.CallPhoneHistoryDao;

/**
 * 
 * 在后台拨打电话。
 * 
 * 1.从数据库中查询出当前，设置的联系人和联系电话号码
 * 2.根据查出来的信息，在预计的时候拨打电话
 * 
 * 
 * */

public class CallService extends Service{

	
	private CallPhoneHistoryDao callPhoneHistoryDao;
	private CallBeanInfo currentCallBeanInfo;
	
	@Override
	public IBinder onBind(Intent intent) {
		
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		currentCallBeanInfo = callPhoneHistoryDao.findCurrentCallBean();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		currentCallBeanInfo = callPhoneHistoryDao.findCurrentCallBean();
		
		String name = currentCallBeanInfo.getName();
		String phone_num = currentCallBeanInfo.getPhone_num();
		
		String call_time = currentCallBeanInfo.getCall_time();
		call_time = "201311042255";
		
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmm");
		
		
		Date date = null;
		try {
			date = df.parse(call_time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Intent i = new Intent(this,PhoneIncomingActivity.class);
		PendingIntent sender=PendingIntent.getBroadcast(this, 0, i, 0);
		
		AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);   
		am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 5*1000, sender); 

		
		
		
		return super.onStartCommand(intent, flags, startId);
	}
	
	
	

}
